/**
 * 接口配置
//  */
// const host = "https://pw.wohongkeji.com/test/"; //餐饮测试环境
// const file = "https://pw.wohongkeji.com/test/file"; //测试环境
const host = "https://pw.wohongkeji.com/prod/"; //正式环境
const file = "https://pw.wohongkeji.com/file"; //正式环境

uni.setStorageSync("foodHost", host);
// 图片路径
// const file = "https://ypsl.wohongkeji.com/"

uni.setStorageSync("foodFile", file);

import user from "../store/modules/user.js";
import { USER_IS_LOGIN } from "../store/mutation_types.js";
// const host = 'http://39.97.180.116:11002/'
export const requestAjax = (url, data = {}, method, showLoading = true) => {
  return new Promise((resolve, reject) => {
    const orgId = uni.getStorageSync("foodOrgId");
    let loadingTimeoutId;
    if (showLoading) {
      loadingTimeoutId = setTimeout(function () {
        uni.showLoading({
          title: "加载中",
        });
      }, 250);
    }

    try {
      const frist_token = uni.getStorageSync("foodToken") || "";

      const header = {
        Authorization: frist_token,
      };

      // if (method == "GET") {
      data.orgId = orgId;
      // }

      const request = uni.request({
        url: host + url,
        data: data,
        method: method,
        header: header,
        success: (res) => {
          if (loadingTimeoutId) {
            clearTimeout(loadingTimeoutId);
          }
          uni.hideLoading();
          // 0 成功 -1 失败 -2 登录失效
          if (res.data.code == 0) {
            // 1 获取数据成功
            resolve(res.data);
          } else {
            // 清空登录信息
            if (
              [
                "AUTHORITY_ERROR_01",
                "AUTHORITY_ERROR_02",
                "AUTHORITY_ERROR_03",
              ].includes(res.data.code)
            ) {
              const { mutations, state } = user;
              uni.setStorageSync("token", "");
              uni.setStorageSync("userInfo", {});

              // 中断其他的请求
              request.abort();
              mutations[USER_IS_LOGIN](state, {});

              uni.showToast({
                title: "登录失效，请重新登录！",
                icon: "none",
              });
              const page = getCurrentPages();
              // if(page[0].route == 'pages/login/index'){
              // 	return
              // }
              // uni.redirectTo({
              // 	url:'/pages/login/index'
              // })
            } else {
              // 验证图片的不提示
              if (url.indexOf("checkUpStatus") > -1) {
                return;
              }
              if (url.indexOf("order/submit") > -1) {
                resolve(res.data);
              }
              uni.showToast({
                title: res.data.message || "网络错误，请重试!",
                icon: "none",
              });
            }
          }
        },
        fail: (res) => {
          setTimeout(() => {
            uni.hideLoading();
          }, 2000);
          uni.showToast({
            title: "网络错误，请重试!!",
            icon: "none",
          });
        },
      });
    } catch (res) {
      uni.hideLoading();
      uni.showToast({
        title: "网络错误，请重试!!!",
        icon: "none",
      });
    }
  });
};

export { host, file };
